import Vue from 'vue'

const state = {
  filterGun: '', // 过滤枪号(和chosenGun的功能有所区别)
  filterPayType: '', // 过滤支付方式
  chosenGun: '', // 选择的油枪
  gunList: [], // 油枪列表
  fuelList: [], // 油品列表
  toSettleList: [], // 待结算列表
  prePayList: [], // 预付款列表
  gunStatusList: [], // 油枪状态列表
  saleFuelList: [], // 油品销售记录列表
  saleCommodityList: [], // 非油销售记录列表
  unpaidFuelList: [], // 挂单油品列表
  showToSettleListDialog: false, // 显示待结算选择框
  prePayDialogVisible: false, // 显示预付款弹窗
  pendingOrderList: Array(9).fill({}), // 挂起的订单列表
  pendingOrderDialogVisible: false // 挂单弹窗是否显示
}
const mutations = {
  SET_GUNLIST (state, data) {
    state.gunList = data
  },
  SET_FUELLIST (state, data) {
    state.fuelList = data
  },
  SET_CHOSENGUN (state, data) {
    state.chosenGun = data
  },
  SET_TOSETTLELIST (state, data) {
    state.toSettleList = data
  },
  SET_PREPAYLIST (state, data) {
    state.prePayList = data
  },
  SET_GUNSTATUSLIST (state, data) {
    state.gunStatusList = data
  },
  SET_SHOWTOSETTLELISTDIALOG (state, data) {
    state.showToSettleListDialog = data
  },
  SET_SALEFUELLIST (state, data) {
    state.unpaidFuelList = data.filter(x => x.paymentType === 'unpaid' && x.payStatus === 1)
    state.saleFuelList = data.filter(x => x.paymentType !== 'unpaid')
  },
  ADD_FUELSALE_ITEM (state, data) {
    // 判断是否已存在列表中，不存在才添加到列表中
    if (state.saleFuelList.length > 0 && JSON.stringify(state.saleFuelList[0]) === JSON.stringify(data)) {
      console.log('重复推送的数据，不做处理')
    } else {
      var exist = false
      for (var i = 0; i < state.saleFuelList.length; i++) {
        if (state.saleFuelList[i].id === data.id) {
          exist = true
          state.saleFuelList[i] = data
          break
        }
      }
      if (exist) {
        state.saleFuelList = Array.from(new Set([...state.saleFuelList]))
        state.unpaidFuelList = state.saleFuelList.filter(x => x.paymentType === 'unpaid')
      } else {
        state.saleFuelList = Array.from(new Set([data, ...state.saleFuelList]))
        if (data.paymentType === 'unpaid') {
          state.unpaidFuelList = Array.from(new Set([data, ...state.unpaidFuelList]))
        }
      }
    }
  },
  SET_SALECOMMODITYLIST (state, dataList) {
    state.saleCommodityList = dataList
  },
  ADD_SALECOMMODITY_ITEM (state, data) {
    // 判断是否已存在列表中，不存在才添加到列表中
    if (state.saleCommodityList.length > 0 && JSON.stringify(state.saleCommodityList[0]) === JSON.stringify(data)) {
      console.log('重复推送的数据，不做处理')
    } else {
      state.saleCommodityList = Array.from(new Set([data, ...state.saleCommodityList]))
    }
  },
  // 更新油品销售记录的退款状态
  UPDATE_SALE_FUEL_REFUND_STATUS (state, data) {
    for (var i = 0; i < state.saleFuelList.length; i++) {
      if (state.saleFuelList[i].id === data.id) {
        state.saleFuelList[i].payStatus = 0
        state.saleFuelList[i].payStatusName = '已退款'
        break
      }
    }
    state.saleFuelList = Array.from(new Set([...state.saleFuelList]))
    state.unpaidFuelList = state.unpaidFuelList.filter(item => item.id !== data.id)
  },
  UPDATE_SALE_COMMODITY_REFUND_STATUS (state, data) {
    for (var i = 0; i < state.saleCommodityList.length; i++) {
      if (state.saleCommodityList[i].id === data.id) {
        state.saleCommodityList[i].payStatus = 0
        state.saleCommodityList[i].payStatusName = '已退款'
        break
      }
    }
    state.saleCommodityList = Array.from(new Set([...state.saleCommodityList]))
  },
  setPrePayDialogVisible (state, data) {
    state.prePayDialogVisible = data
  },
  setPendingOrderDialogVisible (state, data) {
    state.pendingOrderDialogVisible = data
  },
  // 增加挂单记录
  addPendingOrder (state, payload) {
    Vue.set(state.pendingOrderList, payload.index, payload.data)
  },
  // 删除单条挂单记录
  deletePendingOrder (state, payload) {
    state.pendingOrderList[payload.index] = {}
    Vue.set(state.pendingOrderList, payload.index, {})
  },
  // 清空挂单记录
  clearPendingOrderList (state) {
    state.pendingOrderList = Array(9).fill({})
  },
  setFilterGun (state, gunNo) {
    state.filterGun = gunNo
  },
  setFilterPayType (state, payType) {
    state.filterPayType = payType
  },
  addUnpaidRecord (state, data) {
    // 判断是否已存在列表中，不存在才添加到列表中
    if (state.unpaidFuelList.length > 0 && JSON.stringify(state.unpaidFuelList[0]) === JSON.stringify(data)) {
      console.log('重复推送的数据，不做处理')
    } else {
      var exist = false
      for (var i = 0; i < state.unpaidFuelList.length; i++) {
        if (state.unpaidFuelList[i].id === data.id) {
          exist = true
          state.unpaidFuelList[i] = data
          break
        }
      }
      if (exist) {
        state.unpaidFuelList = Array.from(new Set([...state.unpaidFuelList]))
      } else {
        state.unpaidFuelList = Array.from(new Set([data, ...state.unpaidFuelList]))
      }
    }
  }
}

const getters = {
  filterPayType: state => state.filterPayType,
  filterGun: state => state.filterGun,
  gunList: state => state.gunList,
  fuelList: state => state.fuelList,
  chosenGun: state => state.chosenGun,
  toSettleList: state => state.toSettleList,
  prePayList: state => state.saleFuelList.filter(item => item.fuelRecordSource === 2),
  gunStatusList: state => state.gunStatusList,
  saleFuelList: state => state.saleFuelList.filter(item => item.fuelRecordSource !== 2),
  saleCommodityList: state => state.saleCommodityList,
  toSettleListDialogVisible: state => state.showToSettleListDialog,
  toSettleListAfterFilter: state => state.toSettleList.filter(item => state.chosenGun ? item.gunNo === state.chosenGun : true),
  pendingOrderList: state => state.pendingOrderList,
  prePayDialogVisible: state => state.prePayDialogVisible,
  pendingOrderDialogVisible: state => state.pendingOrderDialogVisible,
  unpaidFuelList: state => state.unpaidFuelList
}

const actions = {
  setGunList ({commit}, payload) {
    commit('SET_GUNLIST', payload)
  },
  setFuelList ({commit}, payload) {
    commit('SET_FUELLIST', payload)
  },
  setChosenGun ({commit}, payload) {
    commit('SET_CHOSENGUN', payload)
  },
  setToSettleList ({commit}, payload) {
    commit('SET_TOSETTLELIST', payload)
  },
  setPrePayList ({commit}, payload) {
    commit('SET_PREPAYLIST', payload)
  },
  setGunStatusList ({commit}, payload) {
    commit('SET_GUNSTATUSLIST', payload)
  },
  // 显示待结算列表选择框
  showToSettleListDialog ({commit}) {
    commit('SET_SHOWTOSETTLELISTDIALOG', true)
  },
  // 隐藏待结算列表选择框
  hideToSettleListDialog ({commit}) {
    commit('SET_SHOWTOSETTLELISTDIALOG', false)
  },
  // 设置油品销售记录
  setSaleFuelList ({commit}, payload) {
    commit('SET_SALEFUELLIST', payload)
  },
  addSaleFuelRecord ({commit}, record) {
    commit('ADD_FUELSALE_ITEM', record)
  },
  setSaleCommodityList ({commit}, payload) {
    commit('SET_SALECOMMODITYLIST', payload)
  },
  addSaleCommodityRecord ({commit}, record) {
    commit('ADD_SALECOMMODITY_ITEM', record)
  },
  // 更新油品消费退款状态
  updateSaleFuelRefundStatus ({commit}, payload) {
    commit('UPDATE_SALE_FUEL_REFUND_STATUS', payload)
  },
  // 更新非油消费退款状态
  updateSaleCommodityRefundStatus ({commit}, payload) {
    commit('UPDATE_SALE_COMMODITY_REFUND_STATUS', payload)
  },
  // 显示预付款弹窗
  showPrePayDialog ({commit}) {
    commit('setPrePayDialogVisible', true)
  },
  // 隐藏预付款弹窗
  hidePrePayDialog ({commit}) {
    commit('setPrePayDialogVisible', false)
  },
  // 显示挂单弹窗
  showPendingOrderDialog ({commit}) {
    commit('setPendingOrderDialogVisible', true)
  },
  // 隐藏挂单弹窗
  hidePendingOrderDialog ({commit}) {
    commit('setPendingOrderDialogVisible', false)
  },
  addPendingOrder ({commit}, payload) {
    commit('addPendingOrder', payload)
  },
  deletePendingOrder ({commit}, payload) {
    commit('deletePendingOrder', payload)
  },
  clearPendingOrderList ({commit}) {
    commit('clearPendingOrderList')
  },
  setFilterGun ({commit}, gunNo) {
    commit('setFilterGun', gunNo)
  },
  setFilterPayType ({commit}, payload) {
    commit('setFilterPayType', payload)
  },
  // 添加单条挂单油品记录
  addUnpaidFuelRecord ({commit}, record) {
    commit('addUnpaidRecord', record)
  },
  // 更新油品消费退款状态
  updateUnpaidFuelRefundStatus ({commit}, payload) {
    commit('UPDATE_UNPAID_FUEL_REFUND_STATUS', payload)
  }

}

export default {
  state,
  getters,
  mutations,
  actions
}
